package com.ruoyi.net.utils;

import java.util.List;
import java.util.concurrent.ScheduledFuture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

//@Service
//@EnableScheduling
public class PingService {

    private PingTask pingTask;
    private ScheduledFuture<?> scheduledFuture;
    private TaskScheduler taskScheduler = new ConcurrentTaskScheduler();

    //    @PostConstruct
    public void init() {
        startTask();
    }

    @Autowired
    public PingService(PingTask pingTask) {
        this.pingTask = pingTask;
    }

    // 启动定时Ping任务
    public void startTask() {
        if (scheduledFuture == null || scheduledFuture.isCancelled()) {
            scheduledFuture = taskScheduler.scheduleWithFixedDelay(pingTask::pingDevices, 5000);

        }
    }

    // 停止定时Ping任务
    public void stopTask() {
        if (scheduledFuture != null && !scheduledFuture.isCancelled()) {
            scheduledFuture.cancel(true);
        }
    }
}